﻿
namespace SistemaLogistica.Windows.Forms.Administracion
{
    using System;
    using System.Windows.Forms;
    using EDOSwit.Controls.Helpers;
    using EDOSwit.Controls.Types;
    using EDOSwit.Controls.UserControls;
    using SistemaLogistica.Business.Entities;
    using SistemaLogistica.Business.Logic;
    using SistemaLogistica.Windows.Helpers;

    public partial class CwfAreas : MantenedorSeguro
    {
        public CwfAreas()
        {
            InitializeComponent();
        }

        protected override void LoadPermisos()
        {
            this.TsMenu.Permisos.AddRange(Metodos.GetPermisos(Convert.ToByte(this.Tag)));
        }

        protected override void LoadData(object obj)
        {
            Area area = obj as Area;
           
            if (area != null)
            {
                this.cmbDepartamento.SelectedValue = area.Sucursal.Ubigeo.IdDepartamento;
                this.cmbProvincia.SelectedValue = area.Sucursal.Ubigeo.IdProvincia;
                this.cmbDistrito.SelectedValue = area.Sucursal.Ubigeo.IdDistrito;
                this.cmbSucursal.SelectedValue = area.Sucursal.IdSucursal;
                this.cmbArea.SelectedIndex = -1;
                if (area.IdAreaSuperior != null)
                    this.cmbArea.DataSource = AreaManager.Instancia.ListarPorAreaSuperior(area.IdAreaSuperior.Value);
                this.txtNombre.Text = area.Nombre;
                this.txtDescripcion.Text = area.Descripcion;
                this.identificador = area.IdArea;
            }
        }

        protected override void FocusKeyPress(object sender, KeyPressEventArgs e)
        {
            base.FocusKeyPress(sender, e);
        }

        protected override void InitialLoad()
        {
            try
            {
                this.cmbDepartamento.DisplayMember = "Departamento";
                this.cmbDepartamento.ValueMember = "IdDepartamento";

                this.cmbProvincia.DisplayMember = "Provincia";
                this.cmbProvincia.ValueMember = "IdProvincia";

                this.cmbDistrito.DisplayMember = "Distrito";
                this.cmbDistrito.ValueMember = "IdDistrito";

                this.cmbSucursal.DisplayMember = "Direccion";
                this.cmbSucursal.ValueMember = "IdSucursal";

                this.cmbArea.DisplayMember = "Nombre";
                this.cmbArea.ValueMember = "IdArea";

                this.MantenedorBindingSource.DataSource = AreaManager.Instancia.Listar();
                this.dgvMantenedor.DataSource = this.MantenedorBindingSource;
                this.cmbDepartamento.DataSource = UbigeoManager.Instancia.ListarDepartamentos();
                OcultarColumnas();
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, this.Titulo, ex.Message);
            }
        }

        private void cmbDepartamento_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbDepartamento.SelectedIndex != -1)
            {
                this.cmbProvincia.DataSource = UbigeoManager.Instancia.ListarProvincias(Convert.ToInt32(this.cmbDepartamento.SelectedValue));
                this.cmbProvincia.Enabled = true;
            }
        }

        private void cmbProvincia_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbDepartamento.SelectedIndex != -1)
            {
                this.cmbDistrito.DataSource = UbigeoManager.Instancia.ListarDistritos(Convert.ToInt32(this.cmbDepartamento.SelectedValue), Convert.ToInt32(this.cmbProvincia.SelectedValue));
                this.cmbDistrito.Enabled = true;
            }
        }

        private void cmbDistrito_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cmbDistrito.SelectedIndex != -1)
            {
                int ubigeoID = UbigeoManager.Instancia.ListarIDPorDPD(Convert.ToInt32(this.cmbDepartamento.SelectedValue), Convert.ToInt32(this.cmbProvincia.SelectedValue), Convert.ToInt32(this.cmbDistrito.SelectedValue));
                this.cmbSucursal.DataSource = SucursalManager.Instancia.ListarPorUbigeo(ubigeoID);
                this.cmbSucursal.Enabled = true;
            }
        }

        private void cmbSucursal_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cmbArea.DataSource = AreaManager.Instancia.ListarPosSucursal(Convert.ToInt32(this.cmbSucursal.SelectedValue));
        }

        private void OcultarColumnas()
        {
            this.dgvMantenedor.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dgvMantenedor.Columns["IdArea"].HeaderText = "ID";
            this.dgvMantenedor.Columns["IdAreaSuperior"].Visible = false;
            this.dgvMantenedor.Columns["Sucursal"].Visible = false;
            this.dgvMantenedor.Columns["Descripcion"].Visible = false;
        }

        protected override void BuscarData()
        {
            try
            {
                this.MantenedorBindingSource.DataSource = AreaManager.Instancia.Buscar(this.txtBuscar.Text);
                this.dgvMantenedor.DataSource = this.MantenedorBindingSource;
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, this.Titulo, ex.Message);
            }
        }

        protected override bool SaveData()
        {
            bool result = false;
            int posicion = this.MantenedorBindingSource.Position;
            try
            {
                switch (this.estadoActual)
                {
                    case EstadoActual.Nuevo:
                        AreaManager.Instancia.Agregar(GetArea());
                        break;
                    case EstadoActual.Editar:
                        AreaManager.Instancia.Modificar(GetArea());
                        break;
                    case EstadoActual.Eliminar:
                        AreaManager.Instancia.Eliminar(Convert.ToInt32(this.identificador));
                        posicion -= 1;
                        break;
                    default:
                        break;
                }

                result = true;
                InitialLoad();
                this.MantenedorBindingSource.Position = posicion;
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, this.Titulo, ex.Message);
            }
            return result;
        }

        protected override bool ValidateData()
        {
            bool result = true;

            if (string.IsNullOrEmpty(this.txtNombre.Text.Trim()))
            {
                this.errValidator.SetError(this.txtNombre, "Ingresar Area.");
                result = false;
            }
            else
            {
                this.errValidator.SetError(this.txtNombre, string.Empty);
            }
          
            if (this.cmbDepartamento.SelectedIndex == -1)
            {
                this.errValidator.SetError(this.cmbDepartamento, "Seleccionar Departamento.");
                result = false;
            }
            else
            {
                this.errValidator.SetError(this.cmbDepartamento, string.Empty);
            }
            if (this.cmbProvincia.SelectedIndex == -1)
            {
                this.errValidator.SetError(this.cmbProvincia, "Seleccionar Provincia.");
                result = false;
            }
            else
            {
                this.errValidator.SetError(this.cmbProvincia, string.Empty);
            }
            if (this.cmbDistrito.SelectedIndex == -1)
            {
                this.errValidator.SetError(this.cmbDistrito, "Seleccionar Distrito.");
                result = false;
            }
            else
            {
                this.errValidator.SetError(this.cmbDistrito, string.Empty);
            }
            if (this.cmbSucursal.SelectedIndex == -1)
            {
                this.errValidator.SetError(this.cmbSucursal, "Seleccionar Sucursal.");
                result = false;
            }
            else
            {
                this.errValidator.SetError(this.cmbSucursal, string.Empty);
            }
            
            return result;
        }

        private Area GetArea()
        {
            Area area = new Area();
            area.IdArea = Convert.ToInt32(this.identificador);
            area.Nombre = this.txtNombre.Text.Trim();
            area.Sucursal = new Sucursal { IdSucursal = Convert.ToInt32(this.cmbSucursal.SelectedValue) };
            area.Descripcion = this.txtDescripcion.Text;
            if (this.cmbArea.SelectedIndex != -1)
                area.IdAreaSuperior = Convert.ToInt32(this.cmbArea.SelectedValue);
            return area;
        }

        protected override void EventoNuevo()
        {
            this.cmbDepartamento.Focus();
        }

        protected override void EventoEditar()
        {
            this.cmbDepartamento.Focus();
        }

        protected override void ClearControls()
        {
            this.identificador = 0;
            base.ClearControls();
        }
    }
}
